package com.project.fiveCerealsSavoury.service.impl;

import java.io.UnsupportedEncodingException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.project.fiveCerealsSavoury.dao.OrderDao;
import com.project.fiveCerealsSavoury.domain.Order;
import com.project.fiveCerealsSavoury.domain.OrderState;
import com.project.fiveCerealsSavoury.exception.BizException;
import com.project.fiveCerealsSavoury.service.OrderService;
import com.project.fiveCerealsSavoury.transfer.AddOrder;
import com.project.fiveCerealsSavoury.transfer.Page;
import com.project.fiveCerealsSavoury.transfer.UserPage;
import com.sun.org.apache.xpath.internal.operations.Or;

@Service
@Transactional
public class OrderServiceImpl implements OrderService {
	
	@Autowired
	private OrderDao orderDao;
	
	@Override
	public boolean addOrder(Order order) throws BizException {
		if (order.getRemark() == null) throw new BizException("备注信息不能为空");
		if (order.getUserName() == null) throw new BizException("用户名不能为空");
		if (order.getUserPhone() == null) throw new BizException("手机号不能为空");
		if (order.getGoodName() == null) throw new BizException("商品名称不能为空");
		if (order.getEndAddress() == null) throw new BizException("收货地址不能为空");
		if (order.getGoodNumber() == null) throw new BizException("商品数量不能为空");
		orderDao.addOrder(order);
		return true;
	}

	@Override
	public List<Order> queryStateOrder(Integer userId, OrderState orderState) throws BizException {
		if (orderState == null) throw new BizException("订单状态不能为空");
		List<Order>list=new ArrayList<>();
		List<Order> queryStateOrder = orderDao.queryStateOrder(userId, orderState);
//		if (queryStateOrder == null) throw new BizException("该订单不存在");
		for (Order order : queryStateOrder) {
			if(order.getOrderState() == null){
				order.setOrderState(orderState);
			}
		}
		list.addAll(queryStateOrder);
		return list;
	}

	@Override
	public boolean modifyOrderState(String orderNumber, OrderState orderState) throws BizException {
		if (orderNumber == null) throw new BizException("订单编号不能为空");
		if (orderState == null) throw new BizException("订单状态不能为空");
		orderDao.modifyOrderState(orderNumber, orderState);
		return true;
	}

	@Override
	public boolean removeOrder(String orderNumber, OrderState orderState) throws BizException {
		if (orderNumber == null) throw new BizException("订单编号不能为空");
		if (orderState == null) throw new BizException("订单状态不能为空");
		boolean removeOrder = orderDao.removeOrder(orderNumber, orderState);
		if (removeOrder == false) throw new BizException("未找到该订单");
		return removeOrder;
	}

	@Override
	public Page queryList(UserPage userPages,OrderState orderState) {
		ArrayList<Order> arrayList = new ArrayList<>(); 
		Page jsonPage = new Page();
		List<Order> queryList = orderDao.queryList(orderState);
		int count = orderDao.count();
		if (userPages.getPageIndex() > userPages.getPageSize()) {
			userPages.setPageIndex(userPages.getPageSize());
		}else if (userPages.getPageIndex() < 1) {
			userPages.setPageIndex(1);
		}
		jsonPage.setPageIndex(userPages.getPageIndex());
		int beginIndex = (userPages.getPageIndex() - 1) * userPages.getPageSize();
		int endIndex = beginIndex+userPages.getPageSize();
		if (endIndex > count) {
			endIndex=count;
		}
		int i = 0;
		for (Order arrayL : queryList) {
			if (i >=beginIndex && i < endIndex) {
				arrayList.add(arrayL);
			}
			i++;
		}
		jsonPage.setTotal(count / userPages.getPageSize() + (count % userPages.getPageSize() > 0 ? 1 : 0));
		jsonPage.setList(arrayList);
		return jsonPage;
	}

	@Override
	public Order queryById(Integer id, OrderState orderState) throws BizException {
		if (id == null) throw new BizException("ID不能为空");
		if (orderState == null) throw new BizException("订单状态不能为空");
		Order order = orderDao.queryById(id, orderState);
//		if (order == null) throw new BizException("该订单不存在");
		return order;
	}

	@Override
	public List<Order> queryOrder(OrderState orderState) throws BizException {
		if(orderState == null) throw new BizException("订单状态为空");
		 List<Order> queryOrder = orderDao.queryOrder(orderState);
		 if(queryOrder == null) throw new BizException("没有该订单");
		 List<Order> orders=new ArrayList<>();
		 for (Order order : queryOrder) {
			if(order.getOrderState().equals(orderState)){
				orders.add(order);
			}else if(orderState.equals("显示全部")){
				orders.add(order);
			}
		}
		return queryOrder;
	}

	@Override
	public int counts() {
		int counts = orderDao.counts();
		return counts;
	}

	@Override
	public Integer addToCar(AddOrder addOrder) {
		Order order = new Order();
		order.setGoodNumber(addOrder.getCount());
		order.setGoodName(addOrder.getGoodName());
		order.setGoodPrice(addOrder.getNowPrice());
		order.setOrderState(addOrder.getOrderState());
		order.setOrderTime(addOrder.getOrderTime());
		order.setUserId(addOrder.getUserId());
		return orderDao.addToCar(order);
	}

	@Override
	public Order queryCar(Integer userId,String goodName, OrderState orderState) throws BizException, UnsupportedEncodingException {
		if (goodName == null) throw new BizException("商品名称为空");
		if (orderState == null) throw new BizException("订单状态为空");
		return orderDao.queryCar(userId ,goodName, orderState);
	}

	@Override
	public boolean modifyNumber(Integer id,Integer newNumber) throws BizException {
		if (id == null) throw new BizException("订单id为空");
		orderDao.modifyNumber(id,newNumber);
		return true;
	}

	@Override
	public boolean removeCar(Integer id, OrderState orderState) throws BizException {
		if (id == null) throw new BizException("订单id为空");
		if (orderState == null) throw new BizException("订单状态为空");
		orderDao.removeCar(id, orderState);
		return true;
	}

	@Override
	public Order queryByOrderState(String orderNumber) throws BizException {
		if(orderNumber == null) throw new BizException("订单编号为空");
		return orderDao.queryByOrderState(orderNumber);
	}

	@Override
	public boolean modifyDele(String orderNumber, OrderState orderState, Timestamp orderTime) throws BizException {
		if (orderNumber == null) throw new BizException("订单编号不能为空");
		if (orderState == null) throw new BizException("订单状态不能为空");
		orderDao.modifyDele(orderNumber, orderState, orderTime);
		return true;
	}
}
